import json
import time

import requests

class RequestsSelf:
    def __init__(self):
        self.__headers = {

        }

    def _request_post(self, url, payload):
        try:
            response = requests.post(url, headers=self.__headers, data=json.dumps(payload))

            if response.status_code == 200:
                response_json = response.json()
                if response_json['code'] == 1:
                    return response_json['data']
            else:
                print("请求异常，请分析原因")
                raise Exception(response.status_code, response.text)

        except requests.exceptions.ConnectTimeout as e:
            print(f"请求超时，正在重新发起:{url}|{payload},60s")
            time.sleep(60)
            return self._request_post(url, payload)

        except requests.exceptions.Timeout as e:
            print(f"请求超时，正在重新发起:{url}|{payload},10s")
            time.sleep(10)
            return self._request_post(url, payload)

